package com.zheng.common.type;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * Created by likun on 2018/1/26.
 */
@MappedJdbcTypes({JdbcType.BIGINT})
@MappedTypes({Date.class})
public class Date2BigIntegerTypeHandler extends BaseTypeHandler<Date> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
        ps.setLong(i,parameter.getTime());
    }

    @Override
    public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Long sqlBigint = rs.getLong(columnName);
        if (sqlBigint != null) {
            return new Date(sqlBigint);
        }
        return null;
    }

    @Override
    public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Long sqlBigint = rs.getLong(columnIndex);
        if (sqlBigint != null) {
            return new Date(sqlBigint);
        }
        return null;
    }

    @Override
    public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Long sqlBigint = cs.getLong(columnIndex);
        if (sqlBigint != null) {
            return new Date(sqlBigint);
        }
        return null;
    }
}
